package com.stars.home.activity

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import com.qmuiteam.qmui.kotlin.onClick
import com.stars.home.R
import com.stars.home.adapter.AreaSelectAdapter
import com.stars.home.base.MBRActivity
import com.stars.home.databinding.ActivityAreaselectBinding
import com.stars.home.model.AreaBean
import com.stars.home.vmodel.AreaSelectVModel
import java.lang.Exception

//城市选择
class AreaSelectActivity : MBRActivity<AreaSelectVModel, AreaSelectAdapter, ActivityAreaselectBinding>() {

    var type = 1
    var name = ""
    var province_code = ""//省的编码
    var city_code = ""//市的编码
    var area_code = ""//区的编码
    var cityname = ""

    var chose = 0//1 搜索


    override fun __before() {
        super.__before()

        try {
            chose = intent?.extras?.getInt("chose", 0)!!

        } catch (e: Exception) {
            chose = 0
        }


    }

    override fun listeners() {
        super.listeners()

        binding.llTodo.onClick {
            //去选择

            if (chose == 0) {
                if (!vm.item.value?.level.equals("2")) {

                    toast("必须要到市")
                    return@onClick

                }
                val intent = Intent()
                val bundle = Bundle()
                bundle.putString("city_code", vm.item.value?.id)
                bundle.putString("cityname", vm.item.value?.name)
                intent.putExtras(bundle)
                setResult(Activity.RESULT_OK, intent)
                finish()
            }

        }
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (chose == 1) {
            titleBar()!!.tvRight!!.text = "确定"
            titleBar()!!.tvRight!!.onClick {
                //筛选
                if (TextUtils.isEmpty(name)) {
                    toast("请选择一个省市")
                    return@onClick
                }

                val intent = Intent()
                val bundle = Bundle()
                bundle.putString("name", name)
                bundle.putString("province_code", province_code)
                bundle.putString("city_code", city_code)
                bundle.putString("area_code", area_code)
                bundle.putString("cityname", cityname)
                intent.putExtras(bundle)
                setResult(Activity.RESULT_OK, intent)
                finish()

            }

        }

        adapter().setOnItemClickListener { adapter, view, position ->
            var bean = adapter.getItem(position) as AreaBean

            if (type == 1) {
                vm.id = bean.id
                province_code = bean.id
                name = bean.name
                type++
                onRefresh()
                return@setOnItemClickListener
            }
            if (type == 2) {
                vm.id = bean.id
                city_code = bean.id
                cityname = bean.name
                name += bean.name
                type++

                if (chose == 0) {//选到市--地图

                    val intent = Intent()
                    val bundle = Bundle()
                    bundle.putString("city_code", city_code)
                    bundle.putString("cityname", cityname)
                    intent.putExtras(bundle)
                    setResult(Activity.RESULT_OK, intent)
                    finish()
                }

                onRefresh()
                return@setOnItemClickListener
            }
            if (type == 3) {
                area_code = bean.id
                name += bean.name

                val intent = Intent()
                val bundle = Bundle()
                bundle.putString("name", name)
                bundle.putString("province_code", province_code)
                bundle.putString("city_code", city_code)
                bundle.putString("area_code", area_code)
                bundle.putString("cityname", cityname)
                intent.putExtras(bundle)
                setResult(Activity.RESULT_OK, intent)
                finish()

                return@setOnItemClickListener
            }

        }
    }


    override fun title(): String {
        return getStr(R.string.chose_city)!!
    }

    override fun initAdapter(): AreaSelectAdapter = AreaSelectAdapter(vm)


    override fun binding(): ActivityAreaselectBinding {
        return ActivityAreaselectBinding.inflate(layoutInflater)
    }

    override fun vmClass(): Class<AreaSelectVModel> {
        return AreaSelectVModel::class.java
    }

    override fun needLoadMore(): Boolean {
        return false
    }


}
